import 'package:eplorex/widgets/button/selectablebutton.dart';
import 'package:flutter/material.dart';
import 'dart:math' as Math;

import 'package:flutter/cupertino.dart';

import 'CustomerRadioList.dart';
import 'commonpicker_selectItem.dart';

class CommonPicker {

  static double _calculateHeight(List<CommonPickerSelectItem> options) {
    double result = 80;
    for (CommonPickerSelectItem value in options) {
      if (value.groupName != null) {
        result += 30;
      }
      if (value.options != null && value.options!.isNotEmpty) {
        result += (value.options!.length / 4).ceil() * 60;
      }
    }
    return result;
  }

  static Future<List<CommonPickerSelectItem>?> showSortPicker(
      BuildContext context, Widget panel) {
    return showModalBottomSheet<List<CommonPickerSelectItem>>(
        context: context,
        builder: (context) {
          return Container(
            height: 150,
            color: Colors.white,
            padding: EdgeInsets.all(20),
            child: CustomerRadioList(['时间优先','距离优先']),
          );
        });
  }

  static Future<List<CommonPickerSelectItem>?> showCommonPicker(
      BuildContext context, Widget panel) {
    return showModalBottomSheet<List<CommonPickerSelectItem>>(
        context: context,
        builder: (context) {
          return Container(
            height: 600,
            color: Colors.white,
            // padding: EdgeInsets.all(20),
            child: panel,
          );
        });
  }

  static Future<List<CommonPickerSelectItem>?> showPicker(
      BuildContext context, List<CommonPickerSelectItem> options) {
    return showModalBottomSheet<List<CommonPickerSelectItem>>(
        context: context,
        builder: (context) {
          return Container(
            height: _calculateHeight(options),
            color: Colors.white,
            padding: EdgeInsets.all(20),
            child: Column(
              children: [
                Expanded(
                    child: ListView.builder(
                        itemCount: options.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Container(
                            margin: EdgeInsets.only(top: 20),
                            child: Wrap(
                              spacing: 10,
                              runSpacing: 10,
                              children: options[index]
                                      .options
                                      ?.map((e) => SelectableButton(e))
                                      .toList() ??
                                  [],
                            ),
                          );
                        })),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Flexible(
                        flex: 1,
                        fit: FlexFit.tight,
                        child: TextButton(
                          child: Text("重置"),
                          onPressed: () {},
                          style: ButtonStyle(
                            shape: MaterialStateProperty.all<
                                RoundedRectangleBorder>(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                              ),
                            ),
                            backgroundColor:
                                MaterialStateProperty.all(Colors.grey),
                            foregroundColor:
                                MaterialStateProperty.all(Colors.white),
                          ),
                        )),
                    SizedBox(
                      width: 10,
                    ),
                    Flexible(
                        flex: 2,
                        fit: FlexFit.tight,
                        child: TextButton(
                          child: Text("确认"),
                          onPressed: () {},
                          style: ButtonStyle(
                            shape: MaterialStateProperty.all<
                                RoundedRectangleBorder>(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                              ),
                            ),
                            backgroundColor:
                                MaterialStateProperty.all(Colors.blue),
                            foregroundColor:
                                MaterialStateProperty.all(Colors.white),
                          ),
                        )),
                  ],
                )
              ],
            ),
          );
        });
  }
}
